export const formatCurrentDate = function (format = 'yyyy-MM-dd') {
  const date = new Date();
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');

  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');

  return format
    .replace('yyyy', year)
    .replace('MM', month)
    .replace('dd', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
}
export const formatSomeDate = function (someDate, format = 'yyyy-MM-dd') {
  const date = someDate;
  const year = date.getFullYear();
  const month = (date.getMonth() + 1).toString().padStart(2, '0');
  const day = date.getDate().toString().padStart(2, '0');

  const hours = date.getHours().toString().padStart(2, '0');
  const minutes = date.getMinutes().toString().padStart(2, '0');
  const seconds = date.getSeconds().toString().padStart(2, '0');

  return format
    .replace('yyyy', year)
    .replace('MM', month)
    .replace('dd', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
}
// 日期加减
export const addDays = function (date, days) {
  const result = new Date(date);
  result.setDate(result.getDate() + days);
  return result;
}
export const addMonths = function (date, months) {
  const result = new Date(date);
  result.setMonth(result.getMonth() + months);
  return result;
}
export const calDaysFromDate = function (dateString) {
  const inputDate = new Date(dateString);
  const today = new Date();

  // 忽略时分秒，只保留日期
  inputDate.setHours(0, 0, 0, 0);
  today.setHours(0, 0, 0, 0);

  const timeDiff = today - inputDate;
  const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

  return daysDiff;
}

export const calSecondsFromDate = function (dateString) {
  const inputDate = new Date(dateString);
  const today = new Date();

  // 忽略时分秒，只保留日期
  // inputDate.setHours(0, 0, 0, 0);
  // today.setHours(0, 0, 0, 0);

  const timeDiff = today - inputDate;
  // const daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

  return timeDiff;
}

function formatRelativeDate(inputDate) {
  const date = new Date(inputDate);
  const now = new Date();

  const diffInSeconds = Math.floor((now - date) / 1000);
  const diffInMinutes = Math.floor(diffInSeconds / 60);
  const diffInHours = Math.floor(diffInSeconds / 3600);
  const diffInDays = Math.floor(diffInSeconds / 86400);

  if (diffInSeconds < 60) {
    return '刚刚';
  } else if (diffInMinutes < 5) {
    return '5分钟之前';
  } else if (diffInMinutes < 30) {
    return '半小时前';
  } else if (diffInHours < 1) {
    return `${diffInMinutes}分钟前`;
  } else if (isSameDay(date, now)) {
    return '今天';
  } else if (isYesterday(date, now)) {
    return '昨天';
  } else if (isDayBeforeYesterday(date, now)) {
    return '前天';
  } else if (isSameYear(date, now) && diffInDays < 365) {
    // 同一年，不显示年份
    return formatDateWithMonth(date);
  } else {
    // 不同年，显示年份
    return formatDateWithYear(date);
  }
}
// 判断两个日期是否是同一天
function isSameDay(date1, date2) {
  return (
    date1.getFullYear() === date2.getFullYear() &&
    date1.getMonth() === date2.getMonth() &&
    date1.getDate() === date2.getDate()
  );
}
// 判断两个日期是否是同一年
function isSameYear(date1, date2) {
  return date1.getFullYear() === date2.getFullYear();
}

// 判断日期1是否是日期2的前一天
function isYesterday(date1, date2) {
  const yesterday = new Date(date2);
  yesterday.setDate(date2.getDate() - 1);
  return isSameDay(date1, yesterday);
}

// 判断日期1是否是日期2的前两天
function isDayBeforeYesterday(date1, date2) {
  const dayBeforeYesterday = new Date(date2);
  dayBeforeYesterday.setDate(date2.getDate() - 2);
  return isSameDay(date1, dayBeforeYesterday);
}

// 格式化日期为“5月7日”形式
function formatDateWithMonth(date) {
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return `${month}月${day}日`;
}

// 格式化日期为“2022年5月7日”形式
function formatDateWithYear(date) {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  return `${year}年${month}月${day}日`;
}

// 举例一个特定日期
// const inputDate = '2022-01-27T12:34:56Z'; // 替换成你的特定日期
// const formattedDate = formatRelativeDate(inputDate);
// console.log(formattedDate);


export const CustomDate = {
  formatCurrentDate,
  formatSomeDate,
  addDays,
  addMonths,
  calDaysFromDate,
  formatRelativeDate,
  calSecondsFromDate
}